package com.zxj.camerademo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

import java.lang.ref.WeakReference;

public class DecodeImgTask extends AsyncTask<String,Integer, Bitmap> {
    private WeakReference imageViewReference;

    public DecodeImgTask(ImageView imageView){
        imageViewReference = new WeakReference(imageView);
    }
    private long temp;


    @Override
    protected Bitmap doInBackground(String... strings) {
        temp = System.currentTimeMillis();
        return BitmapFactory.decodeFile(strings[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            ImageView iv = (ImageView) imageViewReference.get();

            Bitmap compressBitmap = BitmapUtils.decodeBitmap(bitmap, 720, 1080);
            iv.setImageBitmap(compressBitmap);
            Log.d("tag", "原始图片大小  "+bitmap.getWidth()+"* "+bitmap.getHeight());
            Log.d("tag", "压缩后图片大小 "+compressBitmap.getWidth()+" * "+compressBitmap.getHeight());
            Log.d("tag", "加载图片耗时 "+(System.currentTimeMillis() - temp));
        }
        super.onPostExecute(bitmap);
    }
}
